// const result = new Promise(function(resolve, reject) {
//   // resolve(100);
//   reject(100);
// });

// result.then(res => {
//   console.log(res, 'resp');
// }).catch(error => {
//   console.log(error, 'error');
// });

// 用 Promise 创建一个ajax函数
function ajax(url) {
  return new Promise(function(resolve, reject) {
    const request = new XMLHttpRequest();
    request.open('GET', url);
    request.responseType = 'json';
    request.onload = function() {
      if (this.status === 200) {
        resolve(this.response);
      } else {
        reject(new Error(this.statusText));
      }
    };
    request.send();
  });
}

ajax('/static/ajax.json')
  .then(resp => {
    console.log(resp);
  }).catch(error => {
    console.log(error);
  });

